PLUGIN.Name = "Target Information"
PLUGIN.DefaultOn = false
PLUGIN.Description = 
[[Target Information - Plug.]]
PLUGIN.Trigger = false


function PLUGIN:LoadParameters()
	self:AddParameter("x_relpos",    { Type = "slider" , Text = "Grid X position", Defaults = "8", Min = "0", Max = "16", Decimals = "0" } )
	self:AddParameter("y_relpos",    { Type = "slider" , Text = "Grid Y position", Defaults = "14", Min = "0", Max = "16", Decimals = "0" } )
	self:AddParameter("padding",      { Type = "slider"   , Text = "Padding", Defaults = "4", Min = "0", Max = "8", Decimals = "0" } )
	
	self:AddParameter("useworldpos",    { Type = "checkbox" , Text = "Rather use World Positionning", Defaults = "0" } )
	
end

function PLUGIN:Load()
	self.CO_Main   =  Color( 0, 0, 0, 128 )
	self.CO_Text   =  Color( 255, 255, 255, 255 )
	self.CO_Memo   =  Color( 164, 164, 164, 255 )
	
	self.LastTime = 0
	self.Active   = true
	
	self.FoundName = ""
	self.FoundSub  = "Target Information"
	self.FoundNum  = ""
	self.PrimaryWidth = 17
	self.EntAtt = nil
	self.PosAtt = Vector(0,0,0)
	
	self.BaseWidth = 300
end

function PLUGIN:Unload()
end

function PLUGIN:_StringNiceNameTransform( stringInput )
	local stringParts = string.Explode("_",stringInput)
	local stringOutput = ""
	for k,part in pairs(stringParts) do
		local len = string.len(part)
		if (len == 1) then
			stringOutput = stringOutput .. string.upper(part)
		elseif (len > 1) then
			stringOutput = stringOutput .. string.Left(string.upper(part),1) .. string.Right(part,len-1)
		end
		if (k ~= #stringParts) then stringOutput = stringOutput .. " " end
	end
	return stringOutput
end

function PLUGIN.HOOK:Think()
	if RealTime() < (self.LastTime + 0.12) then return end
	self.LastTime = RealTime()
	
	self.MyTraceData = utilx.GetPlayerTrace( LocalPlayer(), LocalPlayer():GetCursorAimVector() )
	self.MyTraceData.filter = {LocalPlayer() , LocalPlayer():GetVehicle()}
	self.MyTraceRes = util.TraceLine( self.MyTraceData )
	if (self.MyTraceRes.Hit) and (self.MyTraceRes.HitNonWorld) and ValidEntity(self.MyTraceRes.Entity) and (self.MyTraceRes.Entity ~= LocalPlayer()) then
		local target = self.MyTraceRes.Entity
		local model = target:GetModel()
		
		if target:IsPlayer() then
			self.FoundName = (team.GetName and team.GetName(target:Team())) or "Player"
			if self.FoundName == "Unassigned" then
				self.FoundName = "Community"
			--else
			--	self.SubsituteColor = (team.GetColor and team.GetColor(target:Team())) or nil
			--	self.SubsituteColor.a = 128
			end
			
			self.SubsituteColor = (team.GetColor and team.GetColor(target:Team())) or nil
			self.SubsituteColor.a = 128
				
			self.FoundSub = target:Nick() or self:_StringNiceNameTransform( target:GetClass() )
			
		elseif string.Right(model , 4) == ".mdl" then
			self.FoundName = self:_StringNiceNameTransform( target:GetClass() )
			
			local parts = string.Explode("/", model )
			self.FoundSub = self:_StringNiceNameTransform( string.Left(parts[#parts] , -5) )
			
			self.SubsituteColor = nil
			
		else
			self.FoundName = self:_StringNiceNameTransform( target:GetClass() )
			
			if string.Left(target:GetClass(), 4) == "prop" then
				self.FoundSub = "Prop"	
			elseif string.Left(target:GetClass(), 4) == "func" then
				self.FoundSub = "World Entity"
			else
				self.FoundSub = "Entity"
			end
			
			self.SubsituteColor = nil
			
		end
		self.Active = true
		
		surface.SetFont( "ScoreboardText" )
		do
			local width, height = surface.GetTextSize( self.FoundName )
			self.PrimaryWidth = width + 10 -- 10 = equivalent 5 + 5 from text borders
		end
		self.EntAtt = target
		
	else
		--self.FoundName = ""
		--self.FoundSub  = ""
		--self.FoundNum  = ""
		self.Active = false
		
	end
end

function PLUGIN.HOOK:HUDPaint()
	local ply = LocalPlayer()
	if not (ValidEntity(ply) and ply:Alive()) then return end
	
	local padding = self:GetNumber("padding")
	local X_Ref = 0
	local Y_Ref = 0
	
	if self:GetNumber("useworldpos") > 0 then
		if ValidEntity(self.EntAtt) then self.PosAtt = self.EntAtt:GetPos() end
		self.VTS = self.PosAtt:ToScreen()
		
		X_Ref = self.VTS.x - (self.BaseWidth / 2)
		Y_Ref = self.VTS.y - (17 / 2)
	
	else
		
		
		X_Ref = padding + self:GetNumber("x_relpos") * ((ScrW() - 2 * padding - self.BaseWidth) / 16 )
		Y_Ref = padding + self:GetNumber("y_relpos") * ((ScrH() - 2 * padding - 17 ) / 16 )
		
	end
	
	if self.SubsituteColor then
		surface.SetDrawColor( self.SubsituteColor.r , self.SubsituteColor.g, self.SubsituteColor.b, self.SubsituteColor.a )
		surface.DrawRect( X_Ref                        , Y_Ref, self.PrimaryWidth,  17 )           -- Icons
	end
	surface.SetDrawColor( self.CO_Main.r , self.CO_Main.g, self.CO_Main.b, self.CO_Main.a )
	surface.DrawRect( X_Ref                        , Y_Ref, self.PrimaryWidth,  17 )           -- Icons
	surface.DrawRect( X_Ref + self.PrimaryWidth + 3, Y_Ref, self.BaseWidth - self.PrimaryWidth - 3,  17 ) -- TTT
	surface.DrawRect( X_Ref + self.PrimaryWidth + 3, Y_Ref, self.BaseWidth - self.PrimaryWidth - 3,  17 ) -- TTT
	
	-- Draw my Icons and Text
	surface.SetDrawColor( 255, 255, 255, 255 )
	draw.SimpleText( self.FoundName, "ScoreboardText", X_Ref + 5, Y_Ref, self.Active and self.CO_Text or self.CO_Memo )
	draw.SimpleText( self.FoundSub , "ScoreboardText", X_Ref + (self.PrimaryWidth + 7) * 0.5 + self.BaseWidth / 2, Y_Ref, self.Active and self.CO_Text or self.CO_Memo, TEXT_ALIGN_CENTER )

end

